package com.sh.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.druid.util.StringUtils;

/**
 * 时间工具类
 * */
public class DateUtil {
	//判断是不是日期 格式正确
	public static boolean checkDateFormat(String date,String format){
		if(StringUtils.isEmpty(format)){
			format = "yyyy-MM-dd hh:mm:ss";
		}
		SimpleDateFormat formatter =
				new SimpleDateFormat(format);
		try {
			formatter.parse(date);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return false;
		}

		return true;

	}
	//拿取系统当前时间
	public static String getSysTime(String format){
		if(StringUtils.isEmpty(format)){
			format = "yyyy-MM-dd hh:mm:ss";
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
		LocalDateTime now = LocalDateTime.now();
		return now.format(formatter);
	}
	//拿取  时间跨度中的所有年份 (yyyy-MM)
	public static List<String> getBetweenYear(String startMonth,String endMonth) throws ParseException{
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		List<String> list = new ArrayList<>();
		SimpleDateFormat formatter =
				new SimpleDateFormat("yyyy-MM");

		startCalendar.setTime(formatter.parse(startMonth));
		int startYear = startCalendar.get(Calendar.YEAR);

		endCalendar.setTime(formatter.parse(endMonth));
		int endYear = endCalendar.get(Calendar.YEAR);

		String year = startYear + "";
		for(int i = startYear;i<=endYear; i++){
			if(!list.contains(startYear+"")){
				list.add(startYear+"");
			}
			startYear++;
		}

		return list;
	}

	//拿取 当前日期的 上一个月 月份
	public static String getPreMonth(String dateStr) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		if(dateStr==null){
			calendar.setTime(new Date(System.currentTimeMillis())); // 设置为当前时间

		}else{
			SimpleDateFormat formatter =
					new SimpleDateFormat("yyyy-MM-dd");
			Date parse = formatter.parse(dateStr);
			calendar.setTime(parse); // 设置为当前时间
		}
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月

		SimpleDateFormat rtnFormatter =
				new SimpleDateFormat("yyyy-MM");
		Date date = calendar.getTime();
		String preDate = rtnFormatter.format(date);
		return preDate;
	}

	//拿取 当前日期的 对应月份的 第一天
	public static String getCurMonthFirstDay(String dateStr) throws ParseException{

		if(dateStr==null){
			//为空，返回当前天
			return getSysTime("yyyy-MM-dd");
		}

		String yyyyMM =
				dateStr.substring(0, dateStr.lastIndexOf("-"));

		return yyyyMM + "-01";
	}
	//在当前月份的基础上加上一个月
	public static String addMonth(String dateStr) throws ParseException{
		Calendar calast = Calendar.getInstance();
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM");
		Date parse = sm.parse(dateStr);
		calast.setTime(parse);
		calast.add(Calendar.MONTH, 1);

		String rtnMonth = sm.format(calast.getTime());

		return rtnMonth;
	}


	//拿取 当前年月的最后一天
	//dateStr 格式 yyyy-MM
	public static String getCurMonthLastDay(String dateStr) throws ParseException{
		Calendar calast = Calendar.getInstance();
		if(StringUtils.isEmpty(dateStr)){

			calast.set(Calendar.DAY_OF_MONTH, calast.getActualMaximum(Calendar.DAY_OF_MONTH));

		}else{
			SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM");
			Date parse = sm.parse(dateStr);

			calast.setTime(parse);
			calast.set(Calendar.DAY_OF_MONTH, calast.getActualMaximum(Calendar.DAY_OF_MONTH));
		}

		SimpleDateFormat rtnSm = new SimpleDateFormat("yyyy-MM-dd");
		String last = rtnSm.format(calast.getTime());

		return last.split("-")[2];
	}

	//拿取 当前日期的 前一天
	public static String getPreDay(String dateStr) throws ParseException{

		SimpleDateFormat formatter =
				new SimpleDateFormat("yyyy-MM-dd");
		Date parse = formatter.parse(dateStr);

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse); // 设置为当前时间
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); // 设置为上一天

		Date date = calendar.getTime();
		String preDate = formatter.format(date);
		return preDate;
	}

	//判断是不是 当前月份第一天
	public static boolean isFirstDay(String dateStr) throws ParseException{
		if(dateStr==null){
			return false;
		}
		String dd =
				dateStr.substring(dateStr.lastIndexOf("-")+1, dateStr.length());
		if("01".equals(dd)){
			return true;
		}
		return false;

	}
	//拿取 当前日期的 月份
	public static String getNowDayMonth(String dateStr) throws ParseException{

		SimpleDateFormat formatter =
				new SimpleDateFormat("yyyy-MM");
		Date parse = formatter.parse(dateStr);

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(parse); // 设置为当前时间
		Date date = calendar.getTime();
		String preDate = formatter.format(date);
		return preDate;
	}


	//拿取 当前年份
	public static String getCurYear(String dateStr,String format) throws ParseException{
		if(dateStr==null ||
				"".equals(dateStr)){
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
			LocalDateTime now = LocalDateTime.now();
			return now.format(formatter);

		}else{
			SimpleDateFormat formatter =
					new SimpleDateFormat(format);
			Date parse = formatter.parse(dateStr);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(parse); // 设置为当前时间

			formatter =
					new SimpleDateFormat("yyyy");
			Date date = calendar.getTime();


			String preDate = formatter.format(date);
			return preDate;
		}




	}

}
